<?php
class UserIdentity extends CUserIdentity
{
	private $_id;
	
	public function authenticate()
	{
		$criteria=new CDbCriteria;
		$criteria->condition='username=:userName OR email=:email';
		$criteria->params=array(':userName'=>$this->username,':email'=>$this->username);
		$user = Members::model()->find($criteria);
		
		if ($user===null)
				$this->errorCode=self::ERROR_USERNAME_INVALID;
        else {
			if (empty($user->password)) {
				if ((md5($user->oldsalt . md5($this->password)) === $user->oldpassword) || $user->status == 'I')
					$this->errorCode=self::ERROR_USERNAME_INVALID;
				else {
					$user->updateByPk($user->id, array("password"=>CPasswordHelper::hashPassword($this->password), "lastlogin"=>new CDbExpression('NOW()')));
					$this->_id=$user->id;
					$this->errorCode=self::ERROR_NONE;
				}
			}
			else {
				if (!CPasswordHelper::verifyPassword($this->password, $user->password) || $user->status == 'I')
					$this->errorCode=self::ERROR_USERNAME_INVALID;
				else {
					$user->updateByPk($user->id, array("lastlogin"=>new CDbExpression('NOW()')));
					$this->_id=$user->id;
					$this->errorCode=self::ERROR_NONE;
				}
			}

		}
		return !$this->errorCode;
	}
	
	public function getId()
	{
		return $this->_id;
	}
}